package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.Topic;
import com.cskaoyan.bean.pojo.TopicExample;
import com.cskaoyan.bean.wx.topic.vo.AllTopicVo;
import com.cskaoyan.bean.wx.topic.vo.TopicDetailVo;
import com.cskaoyan.bean.wx.topic.vo.TopicVo;
import com.cskaoyan.bean.wx.topic.vo.TopicsVo;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.service.wx.WxTopicService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Name : WxTopicServiceImpl.java
 * @Time : 2021/8/17 08:46
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
@Transactional
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    TopicMapper topicMapper;

    @Override
    public TopicsVo getTopicList(Integer page, Integer size) {
        List<TopicVo> topicVos = new ArrayList<>();
        PageHelper.startPage(page, size);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        for (Topic topic : topics) {
            TopicVo topicVo = topic2TopicVo(topic);
            topicVos.add(topicVo);
        }
        Integer total = topicMapper.getCount();
        TopicsVo topicsVo = new TopicsVo();
        topicsVo.setData(topicVos);
        topicsVo.setCount(total);
        return topicsVo;

    }

    @Override
    public TopicDetailVo getTopicDetail(Integer id) {
        TopicDetailVo topicDetailVo = new TopicDetailVo();
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
        AllTopicVo topicVo = transferTopic(topics.get(0));
        topicDetailVo.setTopic(topicVo);
        topicDetailVo.setGoods(topicVo.getGoods());
        return topicDetailVo;
    }

    @Override
    public List<AllTopicVo> getTopicRelated(Integer id) {
        List<AllTopicVo> allTopicVos = new ArrayList<>();
        TopicExample topicExample = new TopicExample();
        topicExample.setLimit(5);
        topicExample.setOffset(0);
        TopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andIdBetween(id-10,id+10);
        criteria.andIdNotEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);
        for (Topic topic : topics) {
            AllTopicVo topicVo = transferTopic(topic);
            allTopicVos.add(topicVo);
        }
        return allTopicVos;
    }

    private AllTopicVo transferTopic(Topic topic) {
        AllTopicVo allTopicVo = new AllTopicVo();
        allTopicVo.setId(topic.getId());
        allTopicVo.setTitle(topic.getTitle());
        allTopicVo.setSubtitle(topic.getSubtitle());
        allTopicVo.setPicUrl(topic.getPicUrl());
        allTopicVo.setPrice(topic.getPrice());
        allTopicVo.setReadCount(topic.getReadCount());
        allTopicVo.setSortOrder(topic.getSortOrder());
        allTopicVo.setGoods(topic.getGoods());
        allTopicVo.setAddTime(topic.getAddTime());
        allTopicVo.setUpdateTime(topic.getUpdateTime());
        allTopicVo.setDeleted(topic.getDeleted());
        allTopicVo.setContent(topic.getContent());
        return allTopicVo;
    }

    private TopicVo topic2TopicVo(Topic topic) {
        TopicVo topicVo = new TopicVo();
        topicVo.setId(topic.getId());
        topicVo.setTitle(topic.getTitle());
        topicVo.setSubtitle(topic.getSubtitle());
        topicVo.setPrice(topic.getPrice());
        topicVo.setReadCount(topic.getReadCount());
        topicVo.setPicUrl(topic.getPicUrl());
        return topicVo;
    }
}
